import type { GithubPageType } from '../../../../../../lib/page_handler/platform/github/types'
import type { GithubElements } from '../../../../../../types/constants/personalization/github'
import type { ThemeType } from '../../../../../../types/interfaces'
import { OthersThemeService } from '../../../../../../lib/abstract'
import { PageHandler } from '../../../../../../lib/page_handler'
import {
  BODY_SELECTOR,
  logger,
  PLATFORM_PAGE_TYPES,
  SUPPORTED_PLATFORMS,
} from '../../../../../../types/constants'
import { GithubCommonPage } from './github/common'

/**
 * 页面处理器
 */
const PAGE_HANDLERS: Partial<Record<GithubPageType, any>> = {
}

/**
 * 主题服务测试链接
 */
export class GithubThemeService extends OthersThemeService {
  readonly name: string = 'GithubTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.OTHERS.value
  private static instance: GithubThemeService
  declare readonly elements: GithubElements
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private readonly commonPage: GithubCommonPage = GithubCommonPage.getInstance()
  private currentPageType: GithubPageType | 'default' = this.pageHandler.pages.github.pageType
  readonly wrapperSelector: string = BODY_SELECTOR

  constructor() {
    super(SUPPORTED_PLATFORMS.Github)

    this.updateElementStylesForTheme()
    this.checkPageType(this.currentPageType)
  }

  /**
   * 获取单例实例
   */
  static getInstance(): GithubThemeService {
    if (!GithubThemeService.instance) {
      GithubThemeService.instance = new GithubThemeService()
    }
    return GithubThemeService.instance
  }

  /**
   * 批量初始化页面样式
   */
  updateElementStylesForTheme(): void {
    // 通用样式初始化
    this.commonPage.init()

    // 动态获取当前页面处理器并初始化
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as GithubPageType]
    if (PageHandlerClass) {
      PageHandlerClass.getInstance().init()
    }
    else {
      logger.warn(`未找到 ${this.currentPageType} 页面的处理器`)
    }
  }

  platformStyleVar(_theme: ThemeType): string {
    return ``
  }

  getBaseStyles(_theme: ThemeType): string {
    return ``
  }

  getPageStyles(_theme: ThemeType): string {
    return `
    `
  }

  getMainStyles(_theme: ThemeType): string {
    // 通用样式
    const commonStyles = this.commonPage.getMainStyles()

    // 动态获取当前页面样式
    let pageStyles = ''
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as GithubPageType]
    if (PageHandlerClass) {
      pageStyles = PageHandlerClass.getInstance().getMainStyles()
    }

    return `
/* ===================== 通用样式 ===================== */   
${commonStyles}
/* ===================== 独立样式 ===================== */
${pageStyles}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return ``
  }

  getFixedStyles(_theme: ThemeType): string {
    return ``
  }
}
